var msg=CN;
$(function(){
	init();
	mpage.modalfactory = function(data,position){//生成列表内容 //TODO 需要变化的地方
        $(".tablist").empty();
        tablist.position = position;//设置数据所在位置
        var tdValue =  "",
            rowclass = "",
            td = function(arg){
            return "<td>"+arg+"</td>";           
            },
            settype = function(arg){
            	var keys = ["公共","私人"];
            	return "<span class='label'>"+keys[arg-1]+"</span>";
            } 	
            setstatus = function(arg){
            	var keys = ["活动草稿","活动发布中","活动已停止","已删除活动"];
            	var colors = ["label-info","label-success","label-important","label-warning"];
            	return "<span class='label "+colors[arg]+"'>"+keys[arg]+"</span>";
            },
            setnumber = function(key){
            	var colors = ["label-info","label-success","label-warning","label-important"],
            		c = "";
            	if(key/20>1) c= colors[0];
            	if(key/50>1) c= colors[1];
            	if(key/100>1) c= colors[2];
            	return "<span class='label "+c+"'>"+key+"</span>";
            },
            setButton = function(data){
            	return "<a class='btn btn-info btn-mini getinfo' data-id='"+data.eid+"' status='0' view='off' title='ID:"+data.eid+"' data-code='"+data.qrcode+"'>详情</a>";
            };
        for ( var element in data) {
        	var kk =data[element],
             	u_btn = '<button class="btn btn-info btn-mini" onclick="updatevevnt(this)" type="button"  key="'+kk.eid+'">'+kk.name+'</button>';
             	tdValue =td(setButton(kk))+ 
            	td(u_btn)+
            	td(timeformat(kk.startdate,4)+" ~ "+timeformat(kk.enddate,6))+
            	td(kk.qrcode)+ 
            	td(setnumber(kk.joincount))+ 
            	td(setnumber(kk.followercount))+ 
            	td(settype(kk.type))+
            	td(setstatus(kk.status));
               	rowclass = element%2;//行样式
               $(".tablist").append("<tr class='tr"+rowclass+"'>"+tdValue+"</tr>");
        }
    };
	mpage.init();
}); 

function init(){
	$(".getinfo").live("click",function(){
		var status = $(this).attr("status"),
			eid = $(this).attr("data-id"),
			code =  $(this).attr("data-code"),
			view = $(this).attr("view");
		switch(view){
	     	case "off":
	     		$(this).attr("view","on");
	        	if(status=="0"){
	        		$(this).attr("status","1");
			        var html = "<tr class='newrow'><td colspan='8' id='td"+eid+"'>数据获取中...</td></tr>";
			        $(this).parent().parent().after(html);
			        getinfo(eid,code);
	        	}else{
	        		$(this).parent().parent().next().show();
	        	}
	        	break;
	     	case "on":
	     		$(this).parent().parent().next().hide();
	     		$(this).attr("view","off");
	     		break;
		}		
	});
	
	$(".createapp").live("click",function(){
		var key = $(this).attr("key"),
			code = $(this).attr("code"),
			app = $(this);
		var url = site.web+"my/event/getapp";
		var json = {url:url,eid:key,uid:site.uid,code:code};
		cb = function (data){
			if(data.code==1){
				var arg = data.data;
				//var arg = 'http://www.mediad.cn/mm/files/download/mm.apk';
				var value = '<img src="http://chart.apis.google.com/chart?chs=100x100&amp;cht=qr&amp;chld=L|0&amp;chl='+arg+'" alt="'+arg+'" title="'+arg+'" width="100" height="100">';
				json.link = "<a href='"+arg+"' class='btn margin10'>"+value+"</a>";
				//json.link = "<a class='btn'>"+data.data+"</a>";
				app.parent().parent().append(json.link);
			}else{
				json.link = "<a class='btn'>系统繁忙,请稍后尝试!</a>";
				app.parent().append(json.link);
			}
			app.hide();
		}	
		G(json,cb);	
	});
	
	$(".cdit").click(function(){//cdit part
		var key = $(this).attr("key"),//条件
			cdit = "status",
			icon = '<span class="caret"></span>',//样式
			title = $(this).text();
		$("#cdtion").val("");
		if(key>-1)
			$("#cdtion").val(cdit+" = "+key);
		$(".cdititle").html(title+icon);
		if(mpage){
			mpage.init();
		}
	});
}

function getinfo(id,code){
	
	btngroup = function(value){//组件构造方法
		return '<div class="btn-group margin65l">'+value+'</div>';
	};
	var event = {venue:"0",schedule:"0",guest:"0",content:"0"};
	var keyName = {venue:"个活动场地",schedule:"组活动议程",guest:"位拟邀嘉宾",content:"条活动信息"};
	var html = {venue:"venue",schedule:"schedule",guest:"guest",content:"content"};
	var url = site.web+"my/event/geteventinfo";
	var json = {url:url,eid:id,uid:site.uid};
	
	cb = function (data){
		if(data.code==1){
			$.each(data.data,function(index,value){
				$.each(event,function(i,v){
					if(index==i){
						event[index] = value;
					}
				});
			});
			$.each(html,function(index,value){
				var area = "<a class='btn' onclick='gotopage(\""+html[index]+"\",\""+"?eid="+id+"\")'>"+event[index]+keyName[index]+"</a>";
				//area += "<a class='btn btn-mini' onclick='gotopage(\""+html[index]+"\",\""+"?eid="+id+"\")'>查看</a>";
				html[index] = area;
			});
			html.app = "<a class='btn btn-success createapp' key='"+id+"' code='"+code+"'>生成app</a>";
			$("#td"+id).html(btngroup(html.venue+html.schedule+html.guest+html.content+html.app));
			$("#td"+id).css('line-height',"100px");
		}
	}	
	G(json,cb);	
}

gotopage = function(type,key){
	var web = site.web + "my/";
	web += "event" + type + key;
	window.location = web;
};

updatevevnt = function(obj){
	var i = $(obj).attr("key");
	setMM("edit");
	window.location = site.web+"my/cparty?eid="+i;
};